package java复习202408.E_各种关键字的用法;

import org.junit.jupiter.api.Test;

public class e1_try_catch_finally_Return顺序 {

    @Test
    public void test1(){
        E1 e1 = new E1();
        String s = e1.method1();
        System.out.println("try和finally中哪个返回："+s);

        String s1 = e1.method2();
        System.out.println("catch和finally中哪个返回："+s1);

        String s2 = e1.method3();
        System.out.println("catch中的异常和finally中的return哪个返回："+s2); // finally 中的return会覆盖掉 catch中抛出异常

    }


    public class E1{
        public String method1() {
            try {
                String result1 = "try";
                System.out.println("try end");
                return result1;
            } finally {
                String result3 = "finally";
                return result3;
            }
        }

        public String method2() {
            try {
                throw new Exception("try中的异常");
            } catch (Exception e) {
                String result2 = "catch";
                System.out.println(result2 + " end");
                return result2;
            } finally {
                String result3 = "finally";
                return result3;
            }
        }

        public String method3() {
            try {
                throw new Exception("try中的异常");
            } catch (Exception e) {
                String result2 = "catch";
                System.out.println(result2 + " end");
                throw new Exception("catch中的异常");
            } finally {
                String result3 = "finally";
                return result3;
            }
        }


        public String method4() throws Exception {
            try {
                String result1 = "try";
                if (result1.equals("try")){
                    throw new Exception("try中的异常");
                }
                return result1;
            } catch (Exception e) {
                String result2 = "catch";
                System.out.println(result2 + " end");
                throw new Exception("catch中的异常");
            } finally {
                String result3 = "finally";
            }
        }
    }



}
